<?php
/**
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     insert.module_options.php
 * Type:     time
 * Name:     time
 * Author:   王超, <wisoch@hotmail.com>
 * Purpose:  select下拉列表
 * -------------------------------------------------------------
 */
function smarty_insert_module_options($params, &$smarty) {
	$data = $params['data']; //提取数据
	if(!is_array($data)) {
		trigger_error("module_options: selected attribute is not an array", E_USER_NOTICE);
		return ;
	}
	if(!$data) return ;

	$selected = isset($params['selected'])? $params['selected']: '';
	$key = isset($params['key'])? $params['key']: '';
	$value = isset($params['value'])? $params['value']: '';
	
	//重组数据
	$_data = array();
	foreach($data as $k => $v) {
		$_data[$v['pid']][] = $v;
	}
	//test();
	return _options($_data, $selected, $key, $value);
}
function _options($data, $selected, $key, $value, $level = 0) {
	$option = '';
	$pad = '';
	$n=1;
	//foreach ($data as $k => $v) {
	while (list($k, $v) = each($data)) {
		foreach ($v as $_v) {
			if($level > 0) $pad = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level).'|__';
			if($_v[$key] == $selected) $default =' selected="select"';
			else $default = '';
			$option .= '<option value="'.$_v[$key].'"'.$default.'>'.$pad.$_v[$value].'</option>';
			if(isset($data[$_v[$key]])) {
				$data = array_slice($data, $n, count($data), true);var_dump($data, '------------');
				$option .= _options(&$data, $selected, $key, $value, $level+1);
				unset($data[$_v[$key]]);
			}
		}
		$n++;
	}
	return $option;
}